home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue24 / survive / FMPYMT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-06-23  |  6.4 KB  |  244 lines

  1. unit fmpymt;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Grids, DBGrids, DB, MultGrid, Mask,
  8.   fmAllo;
  9.  
  10. type
  11.   TfrmPayment = class(TForm)
  12.     grpOutstandingCredits: TGroupBox;
  13.     grpPayment: TGroupBox;
  14.     grdPayment: TStringGrid;
  15.     grpTotals: TGroupBox;
  16.     edtTotalToPay: TEdit;
  17.     edtTotalPaid: TEdit;
  18.     edtBalanceDue: TEdit;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Label3: TLabel;
  22.     btnPost: TButton;
  23.     btnCancel: TButton;
  24.     dsCreditsOut: TDataSource;
  25.     btnAllocation: TButton;
  26.     grdCredits: TDBMultiGrid;
  27.     btnSelectAll: TButton;
  28.     btnClearAll: TButton;
  29.     procedure FormDestroy(Sender: TObject);
  30.     procedure btnCancelClick(Sender: TObject);
  31.     procedure grdCreditsSelected(Sender: TObject);
  32.     procedure grdPaymentSetEditText(Sender: TObject; ACol, ARow: Longint;
  33.       const Value: string);
  34.     procedure btnSelectAllClick(Sender: TObject);
  35.     procedure btnClearAllClick(Sender: TObject);
  36.     procedure btnAllocationClick(Sender: TObject);
  37.     procedure btnPostClick(Sender: TObject);
  38.   private
  39.   public
  40.     CustomerNo: LongInt;
  41.     TotalToPay,
  42.     TotalPaid,
  43.     BalanceDue: Double;
  44.  
  45.     procedure PopulateForm;
  46.     procedure UpdateTotals; 
  47.   end;
  48.  
  49. var
  50.   frmPayment: TfrmPayment;
  51.  
  52. function ShowCreditPaymentDlg(aCustomerNo: LongInt): TModalResult;
  53.  
  54. implementation
  55.  
  56. {$R *.DFM}
  57.  
  58. uses
  59.   uBase, dmData;
  60.  
  61. function ShowCreditPaymentDlg(aCustomerNo: LongInt): TModalResult;
  62. begin
  63. (*
  64.   Application.CreateForm(TfrmPayment, frmPayment);
  65.   try
  66. *)
  67.     with frmPayment do begin
  68.       CustomerNo := aCustomerNo;
  69.       PopulateForm;
  70.       Result := ShowModal;
  71.     end;
  72. (*
  73.   finally
  74.     frmPayment.Release;
  75.   end;
  76. *)
  77. end;
  78.  
  79. procedure TfrmPayment.PopulateForm;
  80. var
  81.   I: Integer;
  82. begin
  83.   UpdateTotals;
  84.  
  85.   { Show the outstanding credits for this customer }
  86.   with dmDataModule.qryCreditsOutByCustomer do begin
  87.     ParamByName('CustNo').AsInteger := CustomerNo;
  88.     Open;
  89.   end;
  90.  
  91.   { Setup the payment method grid }
  92.   with grdPayment do begin
  93.     Cells[0, 0] := 'Method';
  94.     Cells[1, 0] := 'Amount';
  95.     with dmDataModule.PaymentMethodsList do begin
  96.       RowCount := Count + 1;
  97.       for I := 0 to Count - 1 do
  98.         Cells[0, I + 1] := Strings[I];
  99.     end;
  100.   end;
  101. end;
  102.  
  103. procedure TfrmPayment.UpdateTotals;
  104. begin
  105.   if TotalToPay = 0 then
  106.     BalanceDue := 0
  107.   else
  108.     BalanceDue := TotalToPay - TotalPaid;
  109.  
  110.   edtTotalToPay.Text := Format(mskCurrency, [TotalToPay]);
  111.   edtTotalPaid.Text := Format(mskCurrency, [TotalPaid]);
  112.   edtBalanceDue.Text := Format(mskCurrency, [BalanceDue]);
  113.   if BalanceDue < 0 then
  114.     edtBalanceDue.Color := clRed
  115.   else
  116.     edtBalanceDue.Color := TGroupBox(edtBalanceDue.Parent).Color;
  117. end;
  118.  
  119. procedure TfrmPayment.FormDestroy(Sender: TObject);
  120. begin
  121.   dmDataModule.qryCreditsOutByCustomer.Close;
  122. end;
  123.  
  124. procedure TfrmPayment.btnCancelClick(Sender: TObject);
  125. begin
  126.   Close;
  127. end;
  128.  
  129. procedure TfrmPayment.grdCreditsSelected(Sender: TObject);
  130. var
  131.   DeltaAmount: LongInt;
  132. begin
  133. (*  if PaymentAllocation.Allocated then PaymentDeallocated := True;*)
  134.  
  135.   { Adjust the total "Credits to Pay" }
  136.   DeltaAmount := dmDataModule.qryCreditsOutByCustomer.FieldByName('BalanceDue').AsInteger;
  137.   if not grdCredits.Selected then DeltaAmount := -DeltaAmount;
  138.   TotalToPay := TotalToPay + DeltaAmount;
  139.   UpdateTotals;
  140.  
  141.   { Add the credit to the allocation data }
  142.   with dmDataModule.qryCreditsOutByCustomer do
  143.     if grdCredits.Selected then
  144.       frmPaymentAllocation.AddCredit(FieldByName('CreditNo').AsInteger,
  145.                                      Trunc(FieldByName('BalanceDue').AsFloat))
  146.     else
  147.       frmPaymentAllocation.DeleteCredit(FieldByName('CreditNo').AsInteger);
  148. end;
  149.  
  150. procedure TfrmPayment.grdPaymentSetEditText(Sender: TObject; ACol,
  151.   ARow: Longint; const Value: string);
  152. var
  153.   I: Integer;
  154. begin
  155.  
  156.   { Update total payment amount }
  157.   TotalPaid := 0;
  158.   with grdPayment do begin
  159.     for I := 1 to RowCount - 1 do
  160.       if Cells[1, I] <> '' then
  161.         TotalPaid := TotalPaid + StrToFloat(Cells[1, I]);
  162.   end;
  163.  
  164.   UpdateTotals;  { Change the display }
  165. end;
  166.  
  167. procedure TfrmPayment.btnSelectAllClick(Sender: TObject);
  168. begin
  169.   grdCredits.SelectAll(True);
  170. end;
  171.  
  172. procedure TfrmPayment.btnClearAllClick(Sender: TObject);
  173. begin
  174.   grdCredits.SelectAll(False);
  175. end;
  176.  
  177. procedure TfrmPayment.btnAllocationClick(Sender: TObject);
  178. begin
  179.   ShowPaymentAllocationDlg;
  180. end;
  181.  
  182. procedure TfrmPayment.btnPostClick(Sender: TObject);
  183. var
  184.   PaymentNo: LongInt;
  185.   Amount,
  186.   TotalPaidThisCredit: LongInt;
  187.   C, P: Integer;
  188. begin
  189.   with dmDataModule.dbDemo do begin
  190.     StartTransaction;
  191.     try
  192.  
  193.       { post the main payment record }
  194.       with dmDataModule.spPaymentSave do begin
  195.         ParamByName('iCustNo').AsInteger := CustomerNo;
  196.         ParamByName('iAmount').AsFloat := TotalPaid;
  197.         ExecProc;
  198.         PaymentNo := ParamByName('oPaymentNo').AsInteger;
  199.       end;
  200.  
  201.       { Post the payment amounts at finest granularity }
  202.       with frmPaymentAllocation do begin
  203.         with grdCredits do begin
  204.  
  205.           { for each credit selected to pay }
  206.           for C := FixedRows to RowCount - 1 do begin
  207.             TotalPaidThisCredit := 0;
  208.  
  209.             { for each payment method for that credit }
  210.             for P := FixedCols to ColCount - 1 do begin
  211.               Amount := GetCellAmount(Cells[P, C]);
  212.               if Amount <> 0 then
  213.                 with dmDataModule.qryPaymentAllocSave do begin
  214.                   ParamByName('PaymentNo').AsInteger := PaymentNo;
  215.                   ParamByName('CreditNo').AsInteger := LongInt(Credits[C - FixedRows]);
  216.                   ParamByName('PayMethodCode').AsString :=
  217.                     PChar(dmDataModule.PaymentMethodsList.Objects[P - FixedCols]);
  218.                   ParamByName('Amount').AsFloat := Amount;
  219.                   Inc(TotalPaidThisCredit, Amount);
  220.                   ExecSQL;
  221.                 end;
  222.             end;
  223.  
  224.             if TotalPaidThisCredit <> 0 then
  225.               with dmDataModule.spPaymentCreditSave do begin
  226.                 ParamByName('iPaymentNo').AsInteger := PaymentNo;
  227.                 ParamByName('iCreditNo').AsInteger := LongInt(Credits[C - FixedRows]);
  228.                 ParamByName('iAmount').AsFloat := TotalPaidThisCredit;
  229.                 ExecProc;
  230.               end;
  231.           end;
  232.         end;
  233.       end;
  234.  
  235.       Commit;
  236.     except
  237.       Rollback;
  238.       raise;
  239.     end;
  240.   end;
  241. end;
  242.  
  243. end.
  244.